package ysq.xyg.host.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostItemLayoutDao;
import ysq.xyg.host.model.dto.HostItemLayoutDto;
import ysq.xyg.host.model.po.HostItem;
import ysq.xyg.host.model.po.HostItemLayout;
import ysq.xyg.host.service.HostItemService;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostItemLayoutVo;
import ysq.xyg.host.service.HostItemLayoutService;

import java.util.Date;
import java.util.List;

/**
 * 房产-楼盘房型service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:18:38
 */
@Service("hostItemLayoutService")
public class HostItemLayoutServiceImpl extends BaseServiceImpl<HostItemLayout> implements HostItemLayoutService {

    @Autowired
    private HostItemService hostItemService;
    @Autowired
    private HostItemLayoutDao hostItemLayoutDao;

    @Autowired
    public void setBaseDao(HostItemLayoutDao hostItemLayoutDao) {
        this.baseDao = hostItemLayoutDao;
    }

    /**
     * 分页查询
     *
     * @param hostItemLayoutVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:18:38
     */
    @Override
    public ReturnEntity page(HostItemLayoutVo hostItemLayoutVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" order_num asc, create_time desc");
        List<HostItemLayout> list = acceptList(hostItemLayoutVo);
        PageInfo<HostItemLayout> page = new PageInfo<>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostItemLayoutVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:18:38
     */
    @Override
    public ReturnEntity addOrUpdate(HostItemLayoutVo hostItemLayoutVo, SysUser currentUser) {
        boolean isUpate = true;
        HostItemLayout hostItemLayout;
        if (StrUtil.isEmpty(hostItemLayoutVo.getId())) {
            hostItemLayout = new HostItemLayout(currentUser);
            isUpate = false;
        } else {
            hostItemLayout = selectByPrimaryKey(hostItemLayoutVo.getId());
            if (ObjectUtil.isNull(hostItemLayout)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostItemLayout.setUpdateTime(new Date());
            hostItemLayout.setUpdateUser(currentUser.getId());
        }
        hostItemLayout.setItemId(hostItemLayoutVo.getItemId());
        hostItemLayout.setName(hostItemLayoutVo.getName());
        hostItemLayout.setDescription(hostItemLayoutVo.getDescription());
        hostItemLayout.setFirstSupply(hostItemLayoutVo.getFirstSupply());
        hostItemLayout.setType(hostItemLayoutVo.getType());
        hostItemLayout.setCoverImage(hostItemLayoutVo.getCoverImage());
        hostItemLayout.setImages(hostItemLayoutVo.getImages());
        hostItemLayout.setContent(hostItemLayoutVo.getContent());
        hostItemLayout.setAllPrice(hostItemLayoutVo.getAllPrice());
        hostItemLayout.setOrderNum(hostItemLayoutVo.getOrderNum());
        int num;
        if (isUpate) {
            num = updateByPrimaryKey(hostItemLayout);
        } else {
            num = insert(hostItemLayout);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        // 将房型id保存进入楼盘表
        String itemId = hostItemLayout.getItemId();
        HostItemLayoutVo hostItemLayoutSearch = new HostItemLayoutVo();
        hostItemLayoutSearch.setItemId(itemId);
        List<HostItemLayout> hostItemLayouts = acceptList(hostItemLayoutSearch);
        if (CollUtil.isNotEmpty(hostItemLayouts)) {
            String layoutType = "";
            for (HostItemLayout entity : hostItemLayouts) {
                if (StringUtils.isNotEmpty(layoutType)) {
                    layoutType += ",";
                }
                layoutType += entity.getType();
            }
            if (StringUtils.isNotEmpty(layoutType)) {
                HostItem hostItem = new HostItem();
                hostItem.setId(itemId);
                hostItem.setLayoutType(layoutType);
                hostItemService.updateByPrimaryKeySelective(hostItem);
            }
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:18:38
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostItemLayout hostItemLayout = selectByPrimaryKey(id);
        if (hostItemLayout != null) {
            hostItemLayout.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostItemLayout.setUpdateTime(new Date());
            hostItemLayout.setUpdateUser(currentUser.getId());
            int num = updateByPrimaryKey(hostItemLayout);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostItemLayoutVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:18:38
     */
    @Override
    public List<HostItemLayout> acceptList(HostItemLayoutVo hostItemLayoutVo){
        Example example = new Example(HostItemLayout.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostItemLayoutVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostItemLayoutVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostItemLayoutVo.getItemId())) {
            criteria.andEqualTo("itemId", hostItemLayoutVo.getItemId());
        }
        example.setOrderByClause(" order_num asc, update_time desc");
        return hostItemLayoutDao.selectByExample(example);
    }

    @Override
    public List<HostItemLayoutDto> selectHostItemLayoutByItemIdList(String itemId) {
        return hostItemLayoutDao.selectHostItemLayoutByItemIdList(itemId);
    }
}
